import { decode } from "cbor-x";

export class Frame {
    buf: number[];

    constructor(buf: number[]) {
        this.buf = buf;
    }

    type() {
        return this.buf[0];    
    }

    seq() {
        return this.buf[1];
    }

    ec() {
        return this.buf[2];
    }

    cmd() {
        return this.buf[2];
    }

    assert() {
        if (this.ec() != 0) {
            const len = this.buf.length;
            const buf = this.buf.slice(3, len - 2);
            const s = decode(Uint8Array.from(buf));
            throw s;
        }
    }

    parse() {
        return decode(Uint8Array.from( this.buf.slice(3, this.buf.length - 2) ))
    }

    id() {
        return (this.type() << 8) + this.seq();
    }
}
